﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ChatBox.Web.UI
{
    public partial class DangKy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnDangKy_Click(object sender, EventArgs e)
        {
            string SQL = "select * from taikhoan where taikhoan ='"+txtTenDangNhap.Text+"'";
            DataSet dulieu = LopKetNoi.TruyVanDuLieu(SQL,"tkdk");
            if (txtTenDangNhap.Text == "" || txtMatKhau.Text == "")
            {
                lblLoi.Text = "Chưa điền đủ thông tin";
            }
            else
            {
                if (dulieu.Tables["tkdk"].Rows.Count > 0)
                {
                    lblLoi.Text = "Tài khoản đã tồn tại";
                }
                else
                {
                    string insert = "insert into taikhoan(taikhoan,matkhau) values('"+txtTenDangNhap.Text+"','"+txtMatKhau.Text+"')";
                    bool bKT = LopKetNoi.CapNhatDuLieu(insert);
                    if (bKT == true)
                    {

                        Session["user"] = txtTenDangNhap.Text;
                        string sqlTK = "select * from taikhoan where taikhoan ='" + Session["user"] + "'";
                        DataSet duLieuTaiKhoan = LopKetNoi.TruyVanDuLieu(sqlTK, "tkdn");
                        Session["iduser"] = duLieuTaiKhoan.Tables["tkdn"].Rows[0][0];
                        Response.Redirect("chat.aspx");
                    }
                }
            }
        }
    }
}